package cn.wolfcode.p2p.business.service.impl;

import cn.wolfcode.p2p.business.domain.Bid;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.domain.PaymentSchedule;
import cn.wolfcode.p2p.business.domain.PaymentScheduleDetail;
import cn.wolfcode.p2p.business.mapper.PaymentScheduleDetailMapper;
import cn.wolfcode.p2p.business.service.IpaymentScheduleDetailService;
import cn.wolfcode.p2p.util.CalculatetUtil;
import cn.wolfcode.p2p.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@Transactional
@Service
public class paymentScheduleDetailServiceImpl implements IpaymentScheduleDetailService {
        @Autowired
        private PaymentScheduleDetailMapper paymentScheduleDetailMapper;
        @Override
        public void createPaymentScheduleDetail(PaymentSchedule ps,BidRequest br) {
                //借款里的每一个对象,等于每一个投资人
                List<Bid> bids = br.getBids();
                for (Bid bid : bids) {
                        PaymentScheduleDetail psd = new PaymentScheduleDetail();
                        psd.setBidAmount(bid.getAvailableAmount());
                        psd.setBidId(bid.getId());
                        psd.setMonthIndex(ps.getMonthIndex());//第几期
                        psd.setDeadline(ps.getDeadline());
                        psd.setBidRequestId(br.getId());
                        //psd.setPayDate(new Date());实际付款时间
                        psd.setReturnType(br.getReturnType());
                        psd.setPaymentScheduleId(ps.getId());
                        psd.setFromLoginInfo(br.getCreateUser());//借款人
                        psd.setToLoginInfoId(bid.getBidUser().getId());//投资人
                        BigDecimal monthlyReceivePrincipal = CalculatetUtil.calMonthlyReceivePrincipal(bid.getAvailableAmount()
                                , br.getBidRequestAmount(), ps.getPrincipal());//单前投标每期拿到的本金
                        psd.setPrincipal(monthlyReceivePrincipal.setScale(Constants.SCALE_STORE, RoundingMode.HALF_UP));//本期获得的本金
                        BigDecimal monthlyReceiveInterest = CalculatetUtil.calMonthlyReceiveInterest(bid.getAvailableAmount()
                                ,br.getBidRequestAmount(),ps.getInterest());//单前投标每期拿到的利息
                        psd.setInterest(monthlyReceiveInterest.setScale(Constants.SCALE_STORE, RoundingMode.HALF_UP));//本期利息
                        psd.setTotalAmount(psd.getInterest().add(psd.getPrincipal()));//本期应获得的总本息
                        paymentScheduleDetailMapper.insert(psd);
                }



        }

        @Override
        public void update(PaymentScheduleDetail psd) {
                paymentScheduleDetailMapper.updateByPrimaryKey(psd);
        }

        @Override
        public void batchUpdateTransferState(Long[] longs, int tansferState) {
                paymentScheduleDetailMapper.batchUpdateTransferState(longs,tansferState);
        }
}
